﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.WebApp._console.core
{
    public partial class usergroup_user_assign : System.Web.UI.Page
    {
        private Guid _selectedUserGroupId;

        protected void Page_Load(object sender, EventArgs e)
        {
            _selectedUserGroupId = new Guid(Request.QueryString["id"]);

            _ctlLinkList.NavigateUrl = string.Format("./usergroup-user-list.aspx?id={0}", _selectedUserGroupId);
            _ctlLinkBack.NavigateUrl = string.Format("./usergroup-list.aspx?id={0}", _selectedUserGroupId);
        }

        #region 配分人员
        protected void _ctlBtnAssign_Click(object sender, EventArgs e)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            foreach (RepeaterItem item in _ctlGridUser.Items)
            {
                CheckBox checkBox = (CheckBox)item.FindControl("_ctlCheck");
                if (checkBox != null && checkBox.Checked)
                {
                    HiddenField hiddenField = (HiddenField)item.FindControl("_ctlTag");
                    Guid userId = new Guid(hiddenField.Value);
                    service.AssignUserToUserGroup(userId, _selectedUserGroupId);
                }
            }
        }
        #endregion

        protected void _ctlBtnSearch_Click(object sender, EventArgs e)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            PagedList<User> users = service.FindUsers(this._ctlEditFilter.Text, 0, 99999);
            _ctlGridUser.DataSource = users;
            _ctlGridUser.DataBind();
        }
    }
}